package com.syxp.pda.action.plan;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.util.TimeFormatUtil;
import com.syxp.pda.core.web.PagingBean;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.Turn;
import com.syxp.pda.service.plan.TurnService;

public class TurnAction extends BaseAction {
	@Resource
	private TurnService turnService;

	private Turn turn;
	private int turnId;
	private String startTime;
	private String endTime;
	private int start ;
	
	private int limit ;

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String load(){
		QueryFilter filter = new QueryFilter() ;
		filter.setPagingBean(new PagingBean(start,limit)) ;
		List<Turn> all = this.turnService.getDao().getAll(filter);
		JsonArray jarray = new JsonArray() ;
		JsonObject result = new JsonObject() ;
		for(Turn turn : all) {
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("turnID",turn.getTurnID() ) ;
			jobj.addProperty("turnOrder", turn.getTurnOrder()) ;
			jobj.addProperty("startTime", TimeFormatUtil.formatWorkTime(turn.getStartTime())) ;
			jobj.addProperty("endTime",  TimeFormatUtil.formatWorkTime(turn.getEndTime())) ;
			jobj.addProperty("remarks", turn.getRemarks()) ;
			if(turn.getClassDuty()!=null) {
				jobj.addProperty("classID", turn.getClassDuty().getClassID()) ;
				jobj.addProperty("claName", turn.getClassDuty().getClaName()) ;
			}
			if(turn.getLine()!=null) {
				jobj.addProperty("chkLineID", turn.getLine().getChkLineID()) ;
				jobj.addProperty("lineName", turn.getLine().getLineName()) ;
			}
			jarray.add(jobj) ;
		}
		result.addProperty("totalProperty",turnService.getDao().getTotalProperty(filter)) ;
		result.add("result", jarray) ;
		result.addProperty("start", start);
		result.addProperty("limit", limit) ;
		this.jsonString = gson.toJson(result) ;
		System.out.println(jsonString) ;
		return SUCCESS ;
	}
	
	public String save() {
		try{
			this.turn.setStartTime(new SimpleDateFormat("HH:mm:ss").parse(startTime));
			this.turn.setEndTime(new SimpleDateFormat("HH:mm:ss").parse(endTime));
			this.turnService.addService(this.turn);
			this.jsonString="{success:true}" ;
		}catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String update() {
		try{
			this.turn.setStartTime(new SimpleDateFormat("HH:mm:ss").parse(startTime));
			this.turn.setEndTime(new SimpleDateFormat("HH:mm:ss").parse(endTime));
			this.turnService.updateService(turn);
			this.jsonString = "{success:true}" ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString = "{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String delete(){
		try{
			this.turnService.removeService(this.turnId);
			this.jsonString = "{success:true}" ;
		}catch(Exception e){
			e.printStackTrace() ;
			this.jsonString = "{success:false}" ;
		}
		return SUCCESS ;
	}

	/**
	 * @return the turnService
	 */
	public TurnService getTurnService() {
		return turnService;
	}

	/**
	 * @param turnService the turnService to set
	 */
	public void setTurnService(TurnService turnService) {
		this.turnService = turnService;
	}

	/**
	 * @return the turn
	 */
	public Turn getTurn() {
		return turn;
	}

	/**
	 * @param turn the turn to set
	 */
	public void setTurn(Turn turn) {
		this.turn = turn;
	}

	/**
	 * @return the turnId
	 */
	public int getTurnId() {
		return turnId;
	}

	/**
	 * @param turnId the turnId to set
	 */
	public void setTurnId(int turnId) {
		this.turnId = turnId;
	}

	/**
	 * @return the startTime
	 */
	public String getStartTime() {
		return startTime;
	}

	/**
	 * @param startTime the startTime to set
	 */
	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	/**
	 * @return the endTime
	 */
	public String getEndTime() {
		return endTime;
	}

	/**
	 * @param endTime the endTime to set
	 */
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	
}
